package org.sundatagroup.aurora.core.entity.particle;

import org.sundatagroup.aurora.core.entity.model.ParticleModel;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * <p>粒子, 即原子配置
 * </p>
 * 
 * @author Zdream
 * @date 2018-06-01
 * @since 0.2.0
 */
public abstract class Particle {
	
	private int id;
	protected JsonObject data;
	protected String desc;
	
	/**
	 * 规则
	 * @return
	 */
	public abstract ParticleRule[] rules();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public JsonObject getData() {
		return data;
	}

	public void setData(JsonObject data) {
		this.data = data;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public ParticleModel toModel() {
		ParticleModel pm = new ParticleModel();
		
		pm.setId(getId());
		pm.setDesc(desc);
		pm.setData(data.toString());
		
		return pm;
	}

    /**
     * @param
     *   am 需要转换的 ParticleModel
     * @return
     *   {@link ParticleModel} 转换为 {@link Particle}
     */
    public static Particle fromModel(ParticleModel pm) {
        return ParticleFactory.build(pm);
    }
	
	/**
	 * 将 ParticleModel 的数据填充到 Particle 中
	 * @param p
	 * @param pm
	 */
	protected static void of(Particle p, ParticleModel pm) {
		p.id = pm.getId();
		
		JsonParser parser = new JsonParser();
		p.data = parser.parse(pm.getData()).getAsJsonObject();
		
		p.desc = pm.getDesc();
	}
	
}
